package org.skyfree.ghyll.tcard.wizard;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbench;
import org.skyfree.ghyll.tcard.core.ITProjectDescription;
import org.skyfree.ghyll.tcard.core.TCardCore;
import org.skyfree.ghyll.tcard.view.WorkspaceExploreView;

public class NewProjectWizard extends Wizard implements INewWizard {

	NewProjectSinglePage newProjectPage = null;
	private IWorkbench workbench;
	
	public NewProjectWizard() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean performFinish() 
	{
		String projectName = newProjectPage.getProjectName();
		String contact = newProjectPage.getContactID();
		String desc = newProjectPage.getDesc();
		ITProjectDescription pd = TCardCore.getDefault().getWorkSpace().newProjectDescription(projectName);
		pd.setContractID(contact);
		pd.setComment(desc);
		pd.setName(projectName);
		try {
			TCardCore.getDefault().getWorkSpace().getProject(projectName).create(pd);
			IViewReference[] refs = this.workbench.getActiveWorkbenchWindow().getActivePage().getViewReferences();
			for(int i=0;i<refs.length;i++)
			{
				IViewReference oneRef = refs[i];
				if(oneRef.getId().equals(WorkspaceExploreView.ID))
				{
					WorkspaceExploreView target = (WorkspaceExploreView)oneRef.getView(true);
					target.getTreeViewer().refresh();
				}
			}
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	
	
	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// TODO Auto-generated method stub
		this.workbench = workbench;
	}

	@Override
	public void addPages() 
	{
		newProjectPage = new NewProjectSinglePage();
		this.addPage(this.newProjectPage);
	}

	
}
